package ${basepackage}.framework.base.util;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.beanutils.PropertyUtils;


<#include "/author.include">
public class Utils {

	public static String convertArrayToSQLString(String[] args,int start, int end){
		
		StringBuffer returnString = new StringBuffer();
		if(args.length == 0){
			return "''";
		}
		if(end > args.length){
			end = args.length;
		}
		for(int i = start;i<end;i++){
			returnString.append(",'" + args[i] + "'");
		}
		
		return returnString.substring(1,returnString.length());
	}
	
	public static String convertArrayToSQLString(int[] args,int start, int end){
		
		StringBuffer returnString =  new StringBuffer();
		if(args.length == 0){
			return "";
		}
		if(end > args.length){
			end = args.length;
		}
		for(int i = start;i<end;i++){
			returnString.append("," + args[i] + "");
		}
		
		return returnString.substring(1,returnString.length());
	}

	public static String printObject(Object o){
		StringBuffer sb = new StringBuffer();
		
		try {
			Field[] fields = o.getClass().getDeclaredFields();
			sb.append("");
			for(int i = 0;i<fields.length;i++){
				String fieldName = fields[i].getName();
				if(fieldName.equals("serialVersionUID")){
					continue;
				}
				String firstChar = fieldName.substring(0, 1);
				
				String getMethodName = "get" + firstChar.toUpperCase() + fieldName.substring(1);
				
				Object value;

				try {
					Method getMethod = o.getClass().getMethod(getMethodName, new Class[]{});
					
					value = getMethod.invoke(o);
					sb.append(fieldName + " = " + value + ",");
				} catch (Exception e) {					
				}

				
				
			}
			
			Class superClass = o.getClass().getSuperclass();
			while(superClass != null){
				fields = superClass.getDeclaredFields();
				for(int i = 0;i<fields.length;i++){
					String fieldName = fields[i].getName();
					if(fieldName.equals("serialVersionUID")){
						continue;
					}
					String firstChar = fieldName.substring(0, 1);
					
					String getMethodName = "get" + firstChar.toUpperCase() + fieldName.substring(1);
					
					Object value;

					try {
						Method getMethod = superClass.getMethod(getMethodName, new Class[]{});
						
						value = getMethod.invoke(o);
						sb.append(fieldName + " = " + value + ",");
					} catch (Exception e) {					
					}
				}
				
				superClass = superClass.getSuperclass();
				
				
			}
			sb.append("");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
		
		
	}
	
	public static String convertBigDecimal(BigDecimal b)
	{
		if(b == null){
			return "0.00";
		}else{
			DecimalFormat formatter = new DecimalFormat("0.00");
			return formatter.format(b.doubleValue());
		}
	}
	
	
	public static void main(String[] arg){
		Date d = new Date(2013-1900,10,22);
		System.out.println("d="+d);
		System.out.println(formatDate(d, ""));
	}
	
	public static String printStringArray(String[] array){
		
		if(array == null){
			return "[]";
		}
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(int i = 0;i<array.length;i++){
			sb.append("\"" + array[i] + "\",");
		}
		sb.append("]");
		
		return sb.toString();
	}
	
	public static String printDoubleArray(double[] array){
		
		if(array == null){
			return "[]";
		}
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(int i = 0;i<array.length;i++){
			sb.append("" + array[i] + ",");
		}
		sb.append("]");
		
		return sb.toString();
	}
	
public static String printIntArray(int[] array){
		
	if(array == null){
		return "[]";
	}
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(int i = 0;i<array.length;i++){
			sb.append("" + array[i] + ",");
		}
		sb.append("]");
		
		return sb.toString();
	}
	
	public static <T> String printArray(T[] array){
		
		if(array == null){
			return "[]";
		}
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(int i = 0;i<array.length;i++){
			sb.append("" + array[i] + ",");
		}
		sb.append("]");
		
		return sb.toString();
	}

	public static boolean isNullOrEmpty(String str){
		if(str == null || str.trim().equals("")){
			return true;
		}
		return false;
	}
	
	
	
	public static String printDoubleArray(double[][] array){
		if(array == null){
			return "[]";
		}
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for(int i = 0;i<array.length;i++){
			sb.append(printDoubleArray(array[i]));
			sb.append(",");
		}
		sb.append("]");
		
		return sb.toString();
	}
	
	public static double[][] parseDoubleListToArray(List<List<Double>> list){
		if(list == null || list.isEmpty()){
			return new double[][]{};
		}
		
		int firstSize = list.size();
		int secondSize = 0;
		for(int i = 0;i<list.size();i++){
			List<Double> tempList = list.get(i);
			if(tempList == null || tempList.isEmpty()){
				continue;
			}
			if(tempList.size() > secondSize){
				secondSize = tempList.size();
			}
			
		}
		
		double[][] valueArray = new double[firstSize][secondSize];
		for(int i = 0;i<list.size();i++){
			List<Double> tempList = list.get(i);
			double[] tempArray = new double[secondSize];
			if(tempList == null || tempList.isEmpty()){
				valueArray[i] = tempArray;
				continue;
			}
			
			for(int j = 0;j<tempList.size();j++){
				tempArray[j] = tempList.get(j);
			}
			valueArray[i] = tempArray;
		}
		
		return valueArray;
		
	}
	
	
	public static String formatDate(Date date,String format,String defaultValue){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if(date == null){
			return defaultValue;
		}
		
		return sdf.format(date);
	}
	
	public static String formatDate(Date date,String defaultValue){
		SimpleDateFormat sdf = new SimpleDateFormat(AppConstant.DEFAULT_DATE_FORMAT);
		if(date == null){
			return defaultValue;
		}
		
		return sdf.format(date);
	}
	
	public static Date convertDate(String date){
		if(Utils.isNullOrEmpty(date)){
			return null;
		}
		try {
			return new SimpleDateFormat(AppConstant.SHORT_DATE_FORMAT).parse(date);
		} catch (ParseException e) {			
			
		}
		return null;
	}
	
	public static String formatBigDecimal(BigDecimal b){
		
		if(b == null){
			return "0.00";
		}else{
			DecimalFormat formatter = new DecimalFormat("#,##0.00");
			return formatter.format(b.doubleValue());
		}
	}
	
	public static String trim(String str){
		if(str == null){
			return "";
		}else{
			return str.trim();
		}
	}
	
	public static void copyProperties(Object dest, Object orig){
		
/*		List<Field> fieldList = new ArrayList<Field>();
		for(Class<?> c = dest.getClass();c != null;c = c.getSuperclass()){
			fieldList.addAll(Arrays.asList(c.getDeclaredFields()));
		}
		System.out.println("The field length is " + fieldList.size());
		for(int i = 0;i<fieldList.size();i++){
			String fieldName = fieldList.get(i).getName();
			if(fieldName.equals("serialVersionUID")){
				continue;
			}
			String firstChar = fieldName.substring(0, 1);
			
			String getMethodName = "get" + firstChar.toUpperCase() + fieldName.substring(1);
			String setMethodName = "set" + firstChar.toUpperCase() + fieldName.substring(1);
			
			Object value;
			try {
				Method getMethod = orig.getClass().getMethod(getMethodName);
				value = getMethod.invoke(orig);
				System.out.println(getMethod.getName() + "=" + value);
				if(value == null){
					continue;
				}
				
				Method setMethod = dest.getClass().getMethod(setMethodName, new Class[] {value.getClass()});
				
				
				setMethod.invoke(dest, value);
			} catch (Exception e) {
//				e.printStackTrace();
			}
		}
*/
	
		try {
			PropertyUtils.copyProperties(dest, orig);
		} catch (Exception e) {
//			e.printStackTrace();
		}
	}
	
	public static void createDirectory(String folderName){
		File file = new File(folderName);
		if(!file.exists()){
			file.mkdirs();
		}
	}
	
	public static void setValue(Object o,String fieldName,Object value){
		String firstChar = fieldName.substring(0, 1);
		String setMethodName = "set" + firstChar.toUpperCase() + fieldName.substring(1);
		
		try {
			Method setMethod = o.getClass().getMethod(setMethodName, Object.class);
			setMethod.invoke(o, value);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static Object getValue(Object o,String fieldName){
		String firstChar = fieldName.substring(0, 1);
		String setMethodName = "get" + firstChar.toUpperCase() + fieldName.substring(1);
		
		try {
			Method setMethod = o.getClass().getMethod(setMethodName);
			Object value = setMethod.invoke(o);
			return value;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}

	}
	
	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
				+ s.substring(19, 23) + s.substring(24);
	}
	
}
